//@Lim Wei Ke A0083402R
package logic;

import java.io.IOException;
import java.util.ArrayList;

import parser.CommandParser;
import storage.Storage;
import global.CommandType.Type;
import global.Methods;
import global.Task;

public class ExecutionHandler {
	
	private static Type mostRecentCommandType;
	private static DisplayCommand mostRecentDisplay;
	private static DisplayCommand secondMostRecentDisplay;
	private static ArrayList<Task> oldTasks =  new ArrayList<Task>(); 
	private static ArrayList<String> displayFileNames = new ArrayList<String>();
	private static ArrayList<Task> updatedTasks;
	private static boolean isUndo;
	
	public ExecutionHandler() {
		oldTasks = new ArrayList<Task>();
		displayFileNames = new ArrayList<String>();
		isUndo = false;
	}

	public static Type getMostRecentCommandType() {
		return mostRecentCommandType;
	}
	
	public static ArrayList<Task> getOldTasks() {
		return oldTasks;
	}

	public static ArrayList<String> getDisplayFileNames() {
		return displayFileNames;
	}

	public static ArrayList<Task> getUpdatedTasks() {
		return updatedTasks;
	}

	public static boolean getIsUndo() {
		return isUndo;
	}
	
	public static void setMostRecentCommandType(Type newMostRecentCommandType) {
		mostRecentCommandType = newMostRecentCommandType;
	}

	public static void setOldTasks(ArrayList<Task> tasksList) {
		oldTasks = tasksList;
	}

	public static void setDisplayFileNames(ArrayList<String> newDisplayFileNames) {
		displayFileNames = newDisplayFileNames;
	}

	public static void setUpdatedTasks(ArrayList<Task> newUpdatedTasks) {
		updatedTasks = newUpdatedTasks;
	}
	
	public static void setIsUndo(boolean newIsUndo) {
		isUndo = newIsUndo;
	}

	public static ArrayList<String> executeCommand(String command) throws Exception {
		Command standardizedCommand = CommandParser.analyzeCommand(command);
		ArrayList<String> feedback = standardizedCommand.execute();
		
		return feedback;
	}
	
	public static DisplayCommand getSecondRecentDisplay(){
		return  secondMostRecentDisplay;
	}
	
	public static void setSecondRecentDisplay(DisplayCommand newDisplay){
		secondMostRecentDisplay = newDisplay;
	}

	public static DisplayCommand getRecentDisplay() {
		return mostRecentDisplay;		
	}
	
	public static void setRecentDisplay(DisplayCommand lastDisplay) {
		mostRecentDisplay = lastDisplay;		
	}

	public static void sortFilename() throws IOException {
		ArrayList<String> undoneTaskFileNames = new ArrayList<String>();
		ArrayList<String> doneTaskFileNames = new ArrayList<String>();
		
		for (int i = 0; i < displayFileNames.size(); i++){
			String currTaskFileName = displayFileNames.get(i);
			Task curTask = Storage.getFileContentAsTask(currTaskFileName);
			if (curTask.getTaskStatus()){
				doneTaskFileNames.add(currTaskFileName);
			} else {
				undoneTaskFileNames.add(currTaskFileName);
			}
		}
		
		displayFileNames = Methods.appendStringList(undoneTaskFileNames, doneTaskFileNames, null, null);	
	}
}
//@Lim Wei Ke A0083402R